package com.jrzh.mvc.service.sys.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jrzh.common.cache.CacheManager;
import com.jrzh.common.exception.ProjectException;
import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.base.convert.BaseConvertI;
import com.jrzh.framework.base.repository.BaseRepository;
import com.jrzh.framework.base.service.impl.BaseServiceImpl;
import com.jrzh.framework.bean.SessionUser;
import com.jrzh.mvc.constants.CacheConstants;
import com.jrzh.mvc.constants.UserSettingConstants;
import com.jrzh.mvc.convert.sys.UserSettingConvert;
import com.jrzh.mvc.model.sys.UserModel;
import com.jrzh.mvc.model.sys.UserSettingModel;
import com.jrzh.mvc.repository.sys.UserSettingRepository;
import com.jrzh.mvc.search.sys.UserSettingSearch;
import com.jrzh.mvc.service.sys.UserSettingServiceI;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.UserSettingView;
@Service("userSettingService")
public class UserSettingServiceImpl extends BaseServiceImpl<UserSettingModel, UserSettingSearch, UserSettingView> implements UserSettingServiceI{
	
	@Autowired
	private UserSettingRepository userSettingRepository;
	
	@Autowired
	private UserSettingConvert userSettingConvert;
	
	@Autowired
	SysServiceManage sysServiceManage;
	
	@Override
	public BaseRepository<UserSettingModel, Serializable> getDao() {
		return userSettingRepository;
	}

	@Override
	public BaseConvertI<UserSettingModel, UserSettingView> getConvert() {
		return userSettingConvert;
	}

	@Override
	public void saveLang(String lang, SessionUser sessionUser) throws ProjectException{
		UserModel user = sysServiceManage.userService.findById(sessionUser.getId());
		if(null == user) throw new ProjectException(I18nHelper.getI18nByKey("message.adjustment_success", sessionUser));
		UserSettingSearch settingSearch = new UserSettingSearch();
		settingSearch.setEqualKey(UserSettingConstants.ADMIN_LANG);
		settingSearch.setEqualUserId(user.getId());
		UserSettingModel setting = sysServiceManage.userSettingService.findBySearch(settingSearch);
		if(null == setting) {
			setting = new UserSettingModel();
			setting.setKey(UserSettingConstants.ADMIN_LANG);
			setting.setUserId(user.getId());
			setting.setValue(lang);
			sysServiceManage.userSettingService.add(setting, sessionUser);
		}else {
			setting.setValue(lang);
			sysServiceManage.userSettingService.edit(setting, sessionUser);
		}
		Map<String, Object> attrs = sessionUser.getAttrs();
		if(null == attrs) attrs = new HashMap<String, Object>();
		attrs.put("setting", sysServiceManage.userSettingService.selectViewByField("userId", user.getId()));
		attrs.put("lang", lang);
		sessionUser.setAttrs(attrs);
		//刷新登陆缓存
		CacheManager.clearOnly(sessionUser.getUuid());
		CacheManager.putCacheInfo(sessionUser.getUuid(), sessionUser, CacheConstants.LOGIN_TIME_OUT);
	}

}
